<template>
  <el-form :model="account"
           :rules="rules"
           ref="accountForm">
    <el-form-item>
      <h1>注册</h1>
    </el-form-item>
    <el-form-item class="input-row"
                  prop="username">
      <el-input name="username"
                placeholder="请输入用户名"
                prefix-icon="iconfont icon-user"
                type="text"
                v-model="account.username"
                @keyup.enter.native="registerHandle"
                clearable />
    </el-form-item>
    <el-form-item class="input-row"
                  prop="password">
      <el-input name="password"
                placeholder="请输入密码"
                prefix-icon="iconfont icon-unlock"
                type="password"
                v-model="account.password"
                @keyup.enter.native="registerHandle"
                clearable />
    </el-form-item>
    <el-form-item class="input-row"
                  prop="checkPwd">
      <el-input name="password"
                placeholder="请再次输入密码"
                prefix-icon="iconfont icon-unlock"
                type="password"
                v-model="account.checkPwd"
                @keyup.enter.native="registerHandle"
                clearable />
    </el-form-item>
    <el-form-item class="input-row"
                  prop="phone">
      <el-input name="phone"
                placeholder="请输入手机"
                prefix-icon="iconfont icon-smartphone"
                type="tel"
                v-model="account.phone"
                @keyup.enter.native="registerHandle"
                clearable />
    </el-form-item>
    <el-form-item class="input-row"
                  prop="email">
      <el-input name="email"
                placeholder="请输入邮箱"
                prefix-icon="iconfont icon-mail"
                type="email"
                v-model="account.email"
                @keyup.enter.native="registerHandle"
                clearable />
    </el-form-item>
    <el-form-item class="btn-row">
      <el-button class="btn"
                 @click="registerHandle"
                 :loading="loading">
        注册
      </el-button>
    </el-form-item>
    <el-form-item class="text-row">
      已有账号？立即 <a class="link-btn"
         @click="()=>{this.$emit('switch',true)}">登录</a>
    </el-form-item>
  </el-form>
</template>

<script>
import { _debounce } from '@/utils/debounce'
// import { encodeMD5 } from '@/utils/encryption'
import validators from '@/utils/validate'
import { postRegister } from '@/api/user'

export default {
  data () {
    var validatePwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else {
        if (this.account.checkPwd !== '') {
          this.$refs.accountForm.validateField('checkPwd')
        }
        callback()
      }
    }
    var validatePwd2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.account.password) {
        callback(new Error('两次输入的密码不一致'))
      } else { callback() }
    }
    return {
      loading: false,
      account: {
        username: '',
        password: '',
        checkPwd: '',
        phone: '',
        email: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 10, message: '用户名长度在4-10个字符', trigger: 'change' }
        ],
        password: [
          { validator: validatePwd, trigger: 'blur' }
        ],
        checkPwd: [
          { validator: validatePwd2, trigger: 'blur' }
        ],
        phone: [
          { validator: validators.phone, trigger: 'blur' }
        ],
        email: [
          { validator: validators.email, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    registerHandle: _debounce(function () {
      this.loading = true
      this.$refs.accountForm.validate((valid) => {
        if (valid) {
          // this.account.password = encodeMD5(this.account.password)
          postRegister(this.account)
            .then((res) => {
              this.loading = false
              console.log(res.data)
              if ('error' in res.data) {
                this.$notify.error({
                  title: '错误',
                  message: res.data.error,
                  position: 'bottom-right'
                })
                this.$refs.accountForm.resetFields()
              } else if ('success' in res.data) {
                this.$notify.success({
                  title: '成功',
                  message: res.data.success,
                  position: 'bottom-right'
                })
                this.$emit('switch', true)
              }
            })
        }
      })
    }, 300)
  }
}

</script>

<style lang="stylus" scoped>
@import './index.styl'
</style>
